package cn.tedu.csmall.product.webapi.controller;

import cn.tedu.csmall.commoms.response.JsonResult;
import cn.tedu.csmall.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.pojo.vo.AlbumAddNewVO;
import cn.tedu.csmall.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.service.IAlbumService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;


@Api(tags = "4. 相册管理模块")
@RestController
@RequestMapping(value = "/albums", produces = "application/json;charset=utf-8")
public class AlbumController {

    @Autowired
    private IAlbumService albumService;

    /**
     * 新增相册
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "增加相册", notes = "需要商品后台【写】权限：/pms/product/update")
    @PostMapping("/add-new")
    public JsonResult<AlbumAddNewVO> addNew(@Valid AlbumAddNewDTO albumAddDTO) {
        Long id = albumService.addNew(albumAddDTO);
        AlbumAddNewVO albumAddVO = new AlbumAddNewVO();
        albumAddVO.setId(id);
        return JsonResult.ok(albumAddVO);
    }

    @ApiOperationSupport(order = 49)
    @ApiOperation("相册列表")
    @GetMapping("/")
    public JsonResult<List<AlbumListItemVO>> list() {
        return JsonResult.ok(albumService.list());
    }

    @ApiOperationSupport(order = 50)
    @ApiOperation("重建缓存")
    @PostMapping("/cache/rebuild")
    public JsonResult<Void> rebuildCache() {
        albumService.rebuildCache();
        return JsonResult.ok();
    }

}
